//
//  CustomViewController.m
//  MeiLiCity
//
//  Created by kwsdzjx on 15/4/23.
//  Copyright (c) 2015年 周佳兴. All rights reserved.
//

#import "CustomViewController.h"
#import "MyButton1.h"
#import "Masonry.h"
#import "UIColor+CL.h"

#import "EducationAdminClient_URL.h"


#define STYLE0 @"0"
#define STYLE1 @"1"

@interface CustomViewController ()
{
    void(^_selectAtIndex)(NSInteger index);

}

@end

@implementation CustomViewController

-(id)initWithFrame:(CGRect)frame andReturnSelectIndex:(void (^)(NSInteger))inter{
    self = [super initWithFrame:frame];
    if (self) {
        [self createMyTabBar:frame];
        _selectAtIndex = inter;
        self.backgroundColor = [UIColor whiteColor];

    }

    return self;
}

-(void)createMyTabBar:(CGRect)frame{
    
    UIImageView *img =[[UIImageView alloc]init];
    
//    img.backgroundColor = [UIColor grayColor];
    [self addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(self.mas_height);
    }];
    img.image = [UIImage imageNamed:@"tabbar3"];
    
//#ifdef Main
    
    if([getUIStyle() isEqualToString:STYLE0]){
    NSLog(@"主项目程序");
//    NSArray *btnTitleArr = @[@"首页", @"功能管理", @"我"];
#pragma mark - 出现课程表
        NSArray *btnTitleArr;
        //这里添加功能判断
        if (getEnglishLanguage()) {
            btnTitleArr = @[@"Home", @"Schedule",@"Me"];
        }else{
            NSLog(@"%@",rolenames());
            if([rolenames() isEqualToString:@"教师"]){
                btnTitleArr = @[@"看板",@"分类",@"课程表",@"我的"];
            }else{
              btnTitleArr = @[@"看板",@"分类",@"我的"];
            }
            
        }
        //@[@"看板", @"课程表",@"我的"];
#pragma mark - 出现课程表end

//    CGFloat W = [UIScreen mainScreen].bounds.size.width;
    for (int i = 0; i < btnTitleArr.count; i++) {
        
    }
    
    UIImageView *line =[[UIImageView alloc]init];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@(1));
    }];
    line.backgroundColor = [UIColor colorWithHex:0xd1d1d1];
    //***创建主页按钮
    MyButton1 *btnMain = [self createButton:btnTitleArr[0] andIndex:0];
    [self addSubview:btnMain];
    //***创建附近按钮
    MyButton1 *btnNear = [self createButton:btnTitleArr[1] andIndex:1];
    [self addSubview:btnNear];
    //***创建搜索按钮
    
    MyButton1 *btnSearch = [self createButton:btnTitleArr[2] andIndex:2];
    [self addSubview:btnSearch];
        
        MyButton1 *btnSearch2;
        
        if([rolenames() isEqualToString:@"教师"]){
            btnSearch2 = [self createButton:btnTitleArr[3] andIndex:3];
            [self addSubview:btnSearch2];
        }
            
        
    
    
    
    
    //***进行相对布局
//    [btnMain mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.mas_left);
//        make.top.equalTo(self.mas_top).offset(1);
//        make.height.equalTo(self.mas_height);
//        make.width.equalTo(@(W/3));
//    }];
//    
//    [btnNear mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(btnMain.mas_right);
//        make.top.equalTo(self.mas_top).offset(1);
//        make.height.equalTo(self.mas_height);
//        make.width.equalTo(@(W/3));
//    }];
//    
//    [btnSearch mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(btnNear.mas_right);
//        make.top.equalTo(self.mas_top).offset(1);
//        make.height.equalTo(self.mas_height);
//        make.width.equalTo(@(W/3));
//    }];
//        
#pragma mark - 出现课程表
//        MyButton1 *btnSearch1 = [self createButton:btnTitleArr[3] andIndex:3];
        
//        [self addSubview:btnSearch1];
        [btnMain mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.top.equalTo(self.mas_top).offset(1);
            make.height.equalTo(self.mas_height);
            make.width.equalTo(btnNear.mas_width);
        }];
        
        [btnNear mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(btnMain.mas_right);
            make.top.equalTo(self.mas_top).offset(1);
            make.height.equalTo(self.mas_height);
            make.width.equalTo(btnMain.mas_width);
        }];
        
        [btnSearch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(btnNear.mas_right);
            make.top.equalTo(self.mas_top).offset(1);
            make.height.equalTo(self.mas_height);
            make.width.equalTo(btnNear.mas_width);
            if(![rolenames() isEqualToString:@"教师"]){
            make.right.equalTo(self.mas_right);
            }


        }];
        if([rolenames() isEqualToString:@"教师"]){
            [btnSearch2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(btnSearch.mas_right);
                make.top.equalTo(self.mas_top).offset(1);
                make.height.equalTo(self.mas_height);
                make.width.equalTo(btnSearch.mas_width);
                make.right.equalTo(self.mas_right);
                
            }];
        }
        
        
        
//        [btnSearch1 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(btnSearch.mas_right);
//            make.top.equalTo(self.mas_top).offset(1);
//            make.height.equalTo(self.mas_height);
//            make.width.equalTo(btnSearch.mas_width);
//            make.right.equalTo(self.mas_right);
//        }];
#pragma mark - 出现课程表end
        
    }else{
//#elif Student
    NSLog(@"学员端程序");
    NSArray *btnTitleArr = @[@"学习", @"分类", @"课程表",@"我的"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTabbar:) name:@"changeTabbar" object:nil];
    
    
    for (int i = 0; i < btnTitleArr.count; i++) {
        
    }
    
    UIImageView *line =[[UIImageView alloc]init];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@(1));
    }];
    line.backgroundColor = [UIColor colorWithHex:0xd1d1d1];
    //***创建主页按钮
    MyButton1 *btnMain = [self createButton:btnTitleArr[0] andIndex:0];
    [self addSubview:btnMain];
    //***创建附近按钮
    MyButton1 *btnNear = [self createButton:btnTitleArr[1] andIndex:1];
    [self addSubview:btnNear];
    //***创建搜索按钮
    
    MyButton1 *btnSearch = [self createButton:btnTitleArr[2] andIndex:2];
    [self addSubview:btnSearch];
    

    //***创建搜索按钮
    MyButton1 *btnSearch1 = [self createButton:btnTitleArr[3] andIndex:3];
    
    [self addSubview:btnSearch1];
    
    //***进行相对布局
    [btnMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.top.equalTo(self.mas_top).offset(1);
        make.height.equalTo(self.mas_height);
        make.width.equalTo(btnNear.mas_width);
    }];
    
    [btnNear mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btnMain.mas_right);
        make.top.equalTo(self.mas_top).offset(1);
        make.height.equalTo(self.mas_height);
        make.width.equalTo(btnMain.mas_width);
    }];
    
    [btnSearch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btnNear.mas_right);
        make.top.equalTo(self.mas_top).offset(1);
        make.height.equalTo(self.mas_height);
        make.width.equalTo(btnNear.mas_width);
    }];
    
    [btnSearch1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btnSearch.mas_right);
        make.top.equalTo(self.mas_top).offset(1);
        make.height.equalTo(self.mas_height);
        make.width.equalTo(btnSearch.mas_width);
        make.right.equalTo(self.mas_right);
    }];
    }
//#elif Teacher
//    NSLog(@"老师端程序");
//    NSArray *btnTitleArr = @[@"首页", @"菜单", @"课程表",@"我"];
//#else
//    NSLog(@"都不是");
//#endif
    

//    [btnMore mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(btnSearch.mas_right);
//        make.top.equalTo(self.mas_top);
//        make.height.equalTo(self.mas_height);
//        make.width.equalTo(btnSearch.mas_width);
//        make.right.equalTo(self.mas_right);
//    }];
  
}

-(MyButton1 *)createButton:(NSString *)btnTitle andIndex:(int)index{
    MyButton1 *btn = [[MyButton1 alloc]init];
    
    btn.Btntag = index;
//#ifdef Main
    
    if([getUIStyle() isEqualToString:STYLE0]){
        NSLog(@"主项目程序");
       [btn setTitleColor:[UIColor colorWithHex:0x45d5ff] forState:UIControlStateSelected];
        
//        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f%d", index+1]] forState:UIControlStateNormal];
//        btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
//        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f1_%d", index+1]] forState:UIControlStateSelected];
//         [btn setTitleColor:[UIColor colorWithHex:0x45d5ff] forState:UIControlStateSelected];
#pragma mark - 出现课程表
        if (index==0) {
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f%d", index+1]] forState:UIControlStateNormal];
            btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f1_%d", index+1]] forState:UIControlStateSelected];
        }else if(index == 1){
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f%d", 2]] forState:UIControlStateNormal];
            btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f1_%d", 2]] forState:UIControlStateSelected];
//        }else if(index == 2){
//            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f%d", 6]] forState:UIControlStateNormal];
//            btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
//            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f1_%d", 6]] forState:UIControlStateSelected];
        }else if(index == 2){
            if([rolenames() isEqualToString:@"教师"]){
                [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f%d", 6]] forState:UIControlStateNormal];
                btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
                [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f1_%d", 6]] forState:UIControlStateSelected];
            }else{
                [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f%d", 3]] forState:UIControlStateNormal];
                btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
                [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f1_%d", 3]] forState:UIControlStateSelected];
            }
            
        }else{
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f%d", 3]] forState:UIControlStateNormal];
            btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"f1_%d", 3]] forState:UIControlStateSelected];
            
        }
#pragma mark - 出现课程表end

    }else{
//#elif Student
        NSLog(@"学员端程序");
        /*汉普森的tabbar图片代码*/
//        [btn setTitleColor:[UIColor colorWithHex:0x1b478e] forState:UIControlStateSelected];//0xE95256
//        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"submenu_see_normal%d", index+1]] forState:UIControlStateNormal];
//        btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
//        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"submenu_my_press%d", index+1]] forState:UIControlStateSelected];
        
        NSLog(@"学员端程序");
        [btn setTitleColor:[UIColor colorWithHex:0xE95256] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"submenu_%d", index+1]] forState:UIControlStateNormal];
        btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"submenu_see%d", index+1]] forState:UIControlStateSelected];
        
    }
//#elif Teacher
//    NSLog(@"老师端程序");
//#else
//    NSLog(@"都不是");
//#endif
   
    if (index == 0) {
        btn.selected = YES;
        btn.titleLabel.backgroundColor = [UIColor orangeColor];
    }
    
    [btn setTitleColor:[UIColor colorWithRed:0x5b/255.0 green:0x5b/255.0 blue:0x5b/255.0 alpha:1] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:btnTitle forState:UIControlStateNormal];
    //***UIControlContentHorizonAlignmentLeft
    btn.titleLabel.font=[UIFont systemFontOfSize:11];
    btn.titleLabel.backgroundColor = [UIColor whiteColor];
//    btn.titleEdgeInsets= UIEdgeInsetsMake(35, -32, 0, 0);
//    btn.imageEdgeInsets = UIEdgeInsetsMake(-9, 20, 0, 0);
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    return btn;

    
}

- (void) setLeft: (CGFloat) newleft
{
    CGRect newframe = self.frame;
    newframe.origin.x = newleft;
    self.frame = newframe;
}




-(void)changeTabbar:(NSNotification *)notice{
    //你也可以把Child设置通知那里增加一个参数userInfo:dic后面跟一个字典或者什么，然后通过notice.userInfo传递过来输出一样可以
    NSNumber *num1=notice.object; //把在Child里定义通知时候的那个object传递过来
    NSInteger intNum = [num1 integerValue];
    NSLog(@"%zd",intNum);
    
    NSLog(@"hold on the baby,%@",num1);
    
    NSLog(@"btnbtn%ld", intNum);
    NSArray *arr = self.subviews;
    for (id obj in arr) {
        if ([obj isKindOfClass:[MyButton1 class]]) {
            MyButton1 *btnRe = (MyButton1 *)obj;
            if (btnRe.Btntag == intNum) {
                btnRe.selected = YES;
            }else{
                btnRe.selected = NO;
            }
        }
    }
    _selectAtIndex(intNum);
    
}
-(void)btnClick:(MyButton1 *)MyBtn{

    NSLog(@"btnbtn%ld", (long)MyBtn.Btntag);
    NSArray *arr = self.subviews;
    for (id obj in arr) {
        if ([obj isKindOfClass:[MyButton1 class]]) {
            MyButton1 *btnRe = (MyButton1 *)obj;
            if (btnRe.Btntag == MyBtn.Btntag) {
                btnRe.selected = YES;
            }else{
                btnRe.selected = NO;
                }
        }
    }
    _selectAtIndex(MyBtn.Btntag);

}

@end
